package gscrapper.main;

import gscrapper.dao.NintendoGameInfoDAO;
import gscrapper.dao.NintendoGameInfoFileDAO;
import gscrapper.dao.SonyGameInfoDAO;
import gscrapper.dao.SonyGameInfoFileDAO;
import gscrapper.service.NintendoService;
import gscrapper.service.SonyService;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class Main {
	private static class Login {
		private String username;
		private String password;

		Login(String username, String password) {
			this.username = username;
			this.password = password;
		}

		public String getUsername() {
			return username;
		}

		public String getPassword() {
			return password;
		}
	}

	private static Login showLoginDialog(String title) {
		JTextField username = new JTextField();
		JPasswordField password = new JPasswordField();
		JOptionPane.showConfirmDialog(null, new Object[] {
				new JLabel("Username"), username, new JLabel("Password"),
				password }, "title", JOptionPane.OK_CANCEL_OPTION);

		return new Login(username.getText(), new String(password.getPassword()));
	}

	private static void scrappSony() {
		SonyService sonyService = new SonyService();
		SonyGameInfoDAO sonyGameInfoDAO = new SonyGameInfoFileDAO();

		Login login = showLoginDialog("Sony PSN account info");
		sonyGameInfoDAO.save(sonyService.scrapp(login.getUsername(),
				login.getPassword()));
	}

	private static void scrappNintendo() {
		NintendoService nintendoService = new NintendoService();
		NintendoGameInfoDAO nintendoGameInfoDAO = new NintendoGameInfoFileDAO();

		Login login = showLoginDialog("Club Nintendo account info");
		nintendoGameInfoDAO.save(nintendoService.scrapp(login.getUsername(),
				login.getPassword()));
	}

	public static void main(String[] args) {
		System.out.println("Welcome");

		// scrappSony();
		scrappNintendo();

		System.out.println("And done!");
	}
}
